﻿using Gfw.BLL.Basic.Account;
using Gfw.Web.Areas.Admin.Models;
using Gfw.Web.Common;
using Gfw.Web.Models.Account;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;


namespace Gfw.Web.Areas.Admin.Controllers
{
    [LAuthorize(RequireRole = "Admin")]
    public class OutlayController : Controller
    {
        #region Session

        /// <summary>
        /// 当前选择的页数
        /// </summary>
        /// 如果是选择参数，也可以这样处理
        int PageIndex
        {
            get { return Session["OutlayController_PageIndex"].ConvertTo<int>(1); }
            set { Session["OutlayController_PageIndex"] = value; }
        }

        private CommonQueryModel QueryModel
        {
            get { return Session["OutlayController_CommonQueryModel"].ConvertTo<CommonQueryModel>(null); }
            set { Session["OutlayController_CommonQueryModel"] = value; }
        }

        #endregion


        #region 方法

        public ActionResult Index(CommonQueryModel model, int? payTypeIndex)
        {
            try
            {
                QueryModel = model;
                PageIndex = 1;
                OutlayPayTypeListModel payTypeList = new OutlayPayTypeListModel();
                ViewBag.PayTypeList = new SelectList(payTypeList.GetOutlayPayTypeList(), "Value", "Key");
                ViewBag.Bonus = SystemAccountLogic.QueryBonus();
            }
            catch (System.Exception ex)
            {
                ModelState.AddModelError("", ex.Message);
            }
            
            return View();
        }

        /// <summary>
        /// 返回指定页面
        /// </summary>
        /// <param name="payTypeIndex">所选下拉</param>
        /// <param name="page">重第一页开始计算</param>
        /// <returns></returns>
        public ActionResult List(int? payTypeIndex, int? page)
        {
            int totalSize;
            int index = page ?? PageIndex;
            if (index < 1) index = 1;

            var list = SystemAccountLogic.QueryOutlay(payTypeIndex??0, QueryModel.StartDate, QueryModel.EndDate, index - 1, ViewHelper.PageSize, out totalSize);
            ViewBag.TotalSize = totalSize;
            PageIndex = ViewBag.PageIndex = index;

            return PartialView(list);
        }


        public ActionResult Create()
        {
            OutlayPayTypeListModel payTypeList = new OutlayPayTypeListModel();
            ViewBag.PayTypeList = new SelectList(payTypeList.GetOutlayPayTypeList(), "Value", "Key");
            ViewBag.Bonus = SystemAccountLogic.QueryBonus();

            return View();
        }

        [AcceptVerbs(HttpVerbs.Post)]
        [ValidateInput(false)]
        [HttpPost]
        public ActionResult Create(OutlayModel outlay, FormCollection collection)
        {
            try
            {
                SystemAccountLogic.CreateOutlay(outlay.PayMoney, int.Parse(outlay.PayType), L.Manage.Authentication.Context.CurrentUser.Id, outlay.PayAbstract);
                return RedirectToAction("Index");
            }
            catch(Exception ex)
            {
                ModelState.AddModelError("", ex.Message);
            }

            OutlayPayTypeListModel payTypeList = new OutlayPayTypeListModel();
            ViewBag.PayTypeList = new SelectList(payTypeList.GetOutlayPayTypeList(), "Value", "Key");
            ViewBag.Bonus = SystemAccountLogic.QueryBonus();

            return View();
        }

        #endregion
    }
}
